package com.jeecg.util;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jeecg.annotation.Controller;
import com.jeecg.annotation.RequestMapping;
import com.jeecg.annotation.RequestParam;
import com.jeecg.bean.vo.MVCVO;

public class AnnotiontionUtil {
	static Map<String,MVCVO>  mapMVC=new HashMap<String,MVCVO> ();
	public static Map<String,MVCVO> getMVCMap(){
		return mapMVC;
	}
	/**
	 * 根据路径获取所有的Ctroll的注解，并获取类注解中的URL，最后存入一个map中
	 * @param basePath
	 * @throws IOException
	 */
	public static void getAnnotationFiles(Set<String> classNames){
		for(String className:classNames){
			Class<?> clazz=null;
			try {
				clazz=Class.forName(className);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			if(clazz.isAnnotationPresent(RequestMapping.class)){
				RequestMapping requestMappingAnno=clazz.getAnnotation(RequestMapping.class);
				String classUrl=requestMappingAnno.value();
				Method []methods=clazz.getDeclaredMethods();
				for(Method m:methods){
					if(!m.isAnnotationPresent(RequestMapping.class))
						continue;
					
					RequestMapping methodMapping=m.getAnnotation(RequestMapping.class);
					//获取方法的url
					String methodUrl=methodMapping.value();
					String requurl=classUrl+methodUrl;
					//方法参数类型
					Class<?> [] pars=m.getParameterTypes();
					//方法参数名称
					List<String> paramsNames=new ArrayList();
					 Annotation[][] params= m.getParameterAnnotations(); 
					 for(Annotation[] annotations:params){
						 boolean flag=false;
						 for(Annotation annotation:annotations){
							 if(annotation instanceof RequestParam){
								 RequestParam an=(RequestParam)annotation;
								 paramsNames.add(an.value());
								 flag=true;
							 }
						 }
						 if(!flag){
							 paramsNames.add("");
						 }
					 }
					 //定义vo，把方法相关属性添加进去
					 MVCVO vo=new MVCVO();
					 vo.setClassType(clazz);
					 vo.setMethodName(m.getName());
					 vo.setParamsClass(pars);
					 vo.setParamsName(paramsNames);
					 //映射存入map中
					 getMVCMap().put(requurl, vo);
				}
			}
		}
	}
	
	
}
